package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.InformationDTO;
import com.example.domain.Commodity;
import com.example.domain.Information;
import com.example.domain.User;
import com.example.service.CommodityService;
import com.example.service.InformationService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InformationController {

	private final InformationService informationService;

	private final CommodityService commodityService;

	private final UserService userService;

	@Autowired
	public InformationController(InformationService informationService,
	                             CommodityService commodityService, UserService userService) {
		this.informationService = informationService;
		this.commodityService = commodityService;
		this.userService = userService;
	}

	/**
	 * 获取一个用户的所有评论信息
	 *
	 * @param userId 用户id
	 * @return 返回查询结果
	 */
	@PostMapping(value = "/comment/user/all", produces = "application/json;charset=utf-8")
	public String myAppraise(Integer userId, Integer currentPage, Integer pageSize) {
		InformationDTO informationDTO = new InformationDTO();
		informationDTO.setUserId(userId).setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reInfoByConditionPage(informationDTO);
	}

	/**
	 * 根据评价信息查询
	 *
	 * @param userId
	 * @param appraiseInformation
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@PostMapping(value = "/comment/user/appraiseInformation", produces = "application/json;charset=utf-8")
	public String myAppraiseByInfo(Integer userId, String appraiseInformation, Integer currentPage, Integer pageSize) {
		InformationDTO informationDTO = new InformationDTO();
		informationDTO.setUserId(userId).setAppraiseInformation(appraiseInformation)
				.setCurrentPage(currentPage).setPageSize(pageSize);
		return this.reInfoByConditionPage(informationDTO);
	}

	/**
	 * 获取一个商品的所有评论信息
	 *
	 * @param commodityId 商品id
	 * @return 返回查询结果
	 */
	@PostMapping(value = "/comment/commodity/all", produces = "application/json;charset=utf-8")
	public String commodityAppraise(Integer commodityId) {
		InformationDTO informationDTO = new InformationDTO();
		informationDTO.setCommodityId(commodityId);
		return this.reInfoByCondition(informationDTO);
	}

	/**
	 * 新增用户评论信息
	 *
	 * @param userId
	 * @param commodityId
	 * @param appraiseInformation
	 * @param appraiseService
	 * @param appraiseScore
	 * @return
	 */
	@PostMapping(value = "/user/information/insert", produces = "application/json;charset=utf-8")
	public String insertComment(Integer userId, Integer commodityId, String appraiseInformation,
	                            Integer appraiseService, Integer appraiseScore) {
		Map<String, Object> message = new HashMap<>(16);
		Commodity commodity = commodityService.getById(commodityId);
		Information information = new Information();
		information.setAppraiseInformation(appraiseInformation);
		information.setAppraiseService(appraiseService);
		information.setAppraiseScore(appraiseScore);
		information.setUserId(userId);
		information.setCommodityId(commodityId);
		information.setMerchantId(commodity.getUserId());
		informationService.save(information);

		//根据用户评分判断是否为好评
		if (appraiseScore >= 3) {
			commodity.setGoodComment(commodity.getGoodComment() + 1);
		}
		Double nowScore = commodity.getAppraiseScore();
		Integer peopleBuy = commodity.getPeopleBuy();
		commodity.setAppraiseScore(nowScore + (appraiseScore - nowScore) / peopleBuy);
		commodityService.updateById(commodity);

		message.put("message", 1);
		message.put("commodityMessage", 1);

		return JSON.toJSONString(message);
	}

	/**
	 * 根据条件查询（用户）
	 *
	 * @param informationDTO 输入条件
	 * @return 返回查询结果的JSON
	 */
	private String reInfoByConditionPage(InformationDTO informationDTO) {
		Map<String, Object> message = new HashMap<>(16);
		List<Map<String, Object>> result = new ArrayList<>();
		Page<Information> page = informationService.getInfoByConditionPage(informationDTO);
		for (Information info : page.getRecords()) {
			Commodity commodity = commodityService.getById(info.getCommodityId());
			Map<String, Object> map = new HashMap<>(16);
			map.put("picture", commodity.getPicture());
			map.put("text", commodity.getText());
			map.put("appraiseService", info.getAppraiseService());
			map.put("appraiseInformation", info.getAppraiseInformation());
			map.put("gmtCreate", info.getGmtCreate());
			result.add(map);
		}
		message.put("information", result);
		message.put("total", page.getTotal());
		return JSON.toJSONString(message);
	}


	/**
	 * 根据条件查询（商家）
	 *
	 * @param informationDTO 输入条件
	 * @return 返回查询结果的JSON
	 */
	private String reInfoByCondition(InformationDTO informationDTO) {
		Map<String, Object> message = new HashMap<>(16);
		List<Map<String, Object>> result = new ArrayList<>();
		List<Information> list = informationService.getInfoByCondition(informationDTO);
		for (Information info : list) {
			User user = userService.getById(info.getUserId());
			Map<String, Object> map = new HashMap<>(16);
			map.put("userName", user.getUserName());
			map.put("appraiseService", info.getAppraiseService());
			map.put("appraiseInformation", info.getAppraiseInformation());
			map.put("gmtCreate", info.getGmtCreate());
			result.add(map);
		}
		message.put("information", result);
		return JSON.toJSONString(message);
	}


}
